﻿using CL.Business.BLL;
using CL.LotteryGameService.Model;
using CL.Tools.Common;
using System;
using System.Configuration;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CL.LotteryGameService.Settlement
{
    public class Handle_Settlement
    {
        protected static string IntervalTime_Settlement = ConfigurationManager.AppSettings["quartz_settlement"] ?? "0 0 03 * * ?";
        protected static string IntervalTime_Settlement_Moth = ConfigurationManager.AppSettings["quartz_settlement_moth"] ?? "0 0 3 01 * ?";
        /// <summary>
        /// 委托启动程序
        /// </summary>
        /// <param name="rich"></param>
        protected delegate void action_delegate(RichTextBox rich);
        /// <summary>
        /// 委托写日志
        /// </summary>
        protected static WritText log_Writ;
        protected QuartzHelper quartzhelper_settlement = new QuartzHelper();
        /// <summary>
        /// 任务调度
        /// </summary>
        public void Run(RichTextBox rich)
        {
            if (rich.InvokeRequired)
            {
                action_delegate dt = new action_delegate(Start);
                rich.Invoke(dt, new object[] { rich });
            }
        }
        /// <summary>
        /// 程序启动
        /// </summary>
        /// <param name="rich"></param>
        protected void Start(RichTextBox rich)
        {
            try
            {
                rich.ForeColor = System.Drawing.Color.Blue;
                log_Writ = new WritText(rich);
                Thread th_Data = new Thread(new ThreadStart(BindData));
                th_Data.Start();
            }
            catch (Exception ex)
            {
                log_Writ.WritTextBox(string.Format("代理分成启动失败：{0}", ex.Message));
                throw;
            }
        }
        protected void BindData()
        {
            string jobname_settlement = "jobname_settlement";
            string jobgroup_settlement = "jobgroup_settlement";
            quartzhelper_settlement.AddTrigger(jobname_settlement, jobgroup_settlement, IntervalTime_Settlement, new Action(() =>
            {
                try
                {
                    this.Execute_Day();
                }
                catch (Exception ex)
                {
                    log_Writ.WritTextBox(string.Format("日代理分成发生错误 --> {0}", ex.StackTrace));
                }
            }));
            jobname_settlement = "jobname_settlement_moth";
            jobgroup_settlement = "jobgroup_settlement_moth";
            quartzhelper_settlement.AddTrigger(jobname_settlement, jobgroup_settlement, IntervalTime_Settlement_Moth, new Action(() =>
            {
                try
                {
                    this.Execute_Moth();
                }
                catch (Exception ex)
                {
                    log_Writ.WritTextBox(string.Format("月代理分成发生错误 --> {0}", ex.StackTrace));
                }
            }));
            Task.Factory.StartNew(() =>
            {
                try
                {
                    quartzhelper_settlement.Start();
                }
                catch (Exception ex)
                {
                    log_Writ.WritTextBox(ex.Message);
                }
            });
        }
        protected void Execute_Day()
        {
            try
            {
                SettlementManagerBLL bll = new SettlementManagerBLL();
                log_Writ.WritTextBox("开始计算用户预计分成");
                //计算用户预计分成
                bll.DoSettlement();
                log_Writ.WritTextBox("计算用户预计分成成功 !!");
                log_Writ.WritTextBox("操作完成!!");
            }
            catch (Exception ex)
            {
                log_Writ.WritTextBox(string.Format("重新计算失败!  等待重新计算!! -> {0}", ex.Message));
            }
        }
        protected void Execute_Moth()
        {
            try
            {
                SettlementManagerBLL bll = new SettlementManagerBLL();
                // 结算用户上个月收入
                log_Writ.WritTextBox("开始计算用户上个月返点");
                bll.DoMonthSettlement();
                log_Writ.WritTextBox("计算用户上个月返点 成功!!");
                log_Writ.WritTextBox("去除推广未达标的用户推广资格");
                bll.DoGetUserNoEffectExtensionBll();
                log_Writ.WritTextBox("操作完成!!");
            }
            catch (Exception ex)
            {
                log_Writ.WritTextBox(string.Format("重新计算失败!  等待重新计算!! -> {0}", ex.Message));
            }
        }

    }
}
